/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package banco;

import java.sql.*;

/**
 *
 * @author eduardo
 */
public class Connect
{
    private Connection conn;
    private Statement stmt;

    public Connect() throws ClassNotFoundException, SQLException
    {
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        Class.forName("oracle.jdbc.driver.OracleDriver");
        try
        {
            conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "a6793121", "a6793121");
            conn.setAutoCommit(true);
            stmt = conn.createStatement();
            System.out.println("Conectado!");
        }
        catch (Exception p)
        {
            System.out.println("Não foi possível criar a conexão!");
        }
    }

    /**
     * create table, drop table, insert, update, delete,...
     */
    public int update(String cmd) throws Exception 
    {
        this.stmt = conn.createStatement();
        int ret = stmt.executeUpdate(cmd);
        stmt.close();
        return ret;
    }

    /**
     * select e outros comandos que retornem dados
     */
    public ResultSet query(String sql) throws Exception 
    {
        this.stmt = conn.createStatement();
        return stmt.executeQuery(sql);
    }

    public void close() throws Exception 
    {
        System.out.println("Desconectado!");
        conn.close();
    }

    public void beginTransaction() throws Exception 
    {
        conn.setAutoCommit(false);
    }

    public void commitTransaction() throws Exception 
    {
        conn.commit();
        conn.setAutoCommit(true);
    }

    public void rollbackTransaction() throws Exception 
    {
        conn.rollback();
        conn.setAutoCommit(true);
    }

    /**
     * @return O Connection desejado
     */
    public Connection getConn()
    {
        return conn;
    }

    /**
     * @param conn Connection a ser setado
     */
    public void setConn(Connection conn)
    {
        this.conn = conn;
    }

    /**
     * @return O Statement desejado
     */
    public Statement getStmt()
    {
        return stmt;
    }

    /**
     * @param stmt Statement a ser setado
     */
    public void setStmt(Statement stmt)
    {
        this.stmt = stmt;
    }
}
